﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Data.GameData.Utils;

namespace Data.GameData
{
    public class Unit : MapObject, ICloneable
    {
        #region Attributes
        private string name;

        private UnitType type;
        private ShipClass shipClass;
        private List<ShipSystem> layout = new List<ShipSystem>();

        private double cost;
        private int htk = 0;

        private Shipyard shipyard = null;
        private bool orders = false;
        #endregion

        #region Constructor
        public Unit(ShipClass sClass, UnitType type)
        {
            shipClass = sClass;
            this.type = type;
        }

        protected Unit()
        {
        }

        private Unit(Unit unit)
        {
        }
        #endregion

        #region Properties
        public UnitType Type
        {
            get { return type; }
            protected set { type = value; }
        }

        public ShipClass ShipClass
        {
            get { return shipClass; }
            set { shipClass = value; }
        }

        public List<ShipSystem> ShipLayout
        {
            get { return layout; }
        }

        public double HullUsed
        {
            get
            {
                double hs = 0;
                foreach (ShipSystem sys in layout)
                    hs += sys.Size;

                return hs;
            }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public double Cost
        {
            get { return cost + (ShipClass.Cost * HullUsed); }
            private set { cost = value; }
        }

        public int HTK
        {
            get { return htk; }
        }

        public bool HasShipyard
        {
            get { return shipyard != null; }
        }

        public Shipyard ShipYard
        {
            get { return shipyard; }
            set { shipyard = value; }
        }

        public bool HasOrders
        {
            get { return orders; }
            set { orders = value; }
        }
        #endregion

        public virtual void AddSystem(ShipSystem sys)
        {
            if (sys != null)
            {
                layout.Add(sys);
                htk += 1;

                cost += sys.Cost;
            }
        }

        public virtual void ClearLayout()
        {
            layout.Clear();
            htk = 0;
            cost = 0;
        }

        public void Parse(string controlString)
        {
            int index = 0;
            string subSystem = "";

            while( index < controlString.Length)
            {
                if (controlString[index] == '(' || controlString[index] == ')')
                    continue;

                subSystem += controlString[index];

                if (index + 1 >= controlString.Length ||
                   controlString[index + 1].ToString().ToUpper() == controlString[index + 1].ToString())
                {
                    AddSystem(GameDataStore.GetShipSystem(subSystem) as ShipSystem);
                    subSystem = "";
                }

                ++index;
            }
        }

        public string ControlString()
        {
            string cString = "";
            foreach (ShipSystem sys in ShipLayout)
                cString += sys.Code;

            return cString;
        }

        public override string ToString()
        {
            return Name;
        }

        #region ICloneable Members
        public override object Clone()
        {
            return this.MemberwiseClone();
        }
        #endregion
    }
}
